﻿using System;
using UnityEngine;

namespace CGF
{
    /// <summary>
    /// 扩展GameObject方法
    /// </summary>
    public static class GameObjectExtend
    {
        public static void AddEnableEvent(this GameObject gameObject, Action action)
        {
            MonoLifeListener listener = gameObject.GetComponent<MonoLifeListener>();
            if (!listener) listener = gameObject.AddComponent<MonoLifeListener>();
            listener.onEnable += action;
        }

        public static void RemoveEnableEvent(this GameObject gameObject, Action action)
        {
            MonoLifeListener listener = gameObject.GetComponent<MonoLifeListener>();
            if (listener)
            {
                listener.onEnable -= action;
            }
        }

        public static void AddDisableEvent(this GameObject gameObject, Action action)
        {
            MonoLifeListener listener = gameObject.GetComponent<MonoLifeListener>();
            if (!listener) listener = gameObject.AddComponent<MonoLifeListener>();
            listener.onDisable += action;
        }

        public static void RemoveDisableEvent(this GameObject gameObject, Action action)
        {
            MonoLifeListener listener = gameObject.GetComponent<MonoLifeListener>();
            if (listener)
            {
                listener.onDisable -= action;
            }
        }

        public static void AddDestroyEvent(this GameObject gameObject, Action action)
        {
            MonoLifeListener listener = gameObject.GetComponent<MonoLifeListener>();
            if (!listener) listener = gameObject.AddComponent<MonoLifeListener>();
            listener.onDestroy += action;
        }

        public static void RemoveDestroyEvent(this GameObject gameObject, Action action)
        {
            MonoLifeListener listener = gameObject.GetComponent<MonoLifeListener>();
            if (listener)
            {
                listener.onDestroy -= action;
            }
        }
    }
}
